xlv.imp.select={
	implement:function(){
	},
	method:{
		select_properties:{
			singleSelection:true,
			cssClassName:'selected',
			_items:[]
		},
		select_enabled:function(enable){
			if(enable)this.addEventHandler('mousedown', '_select_mousedown');
			else this.removeObserve('mousedown', '_select_mousedown');
		},
		_select_mousedown:function(event){
			var item =this.select_selectable(event.target);
			if(item) this.select_item(item===true?event.target:item);
			var frame=this.getFrameById();
			//if(!frame||(frame&&frame.isActivated())) event.stop();
		},
		/* call from mouse down
		 * return false or null -- cannot select,
		 * return true -- item=aE 
		 * return item
		 */
		select_selectable:function(target){
			return true;
		},
		select_changed:function(to_item,from_item){
			var memo=xlv.sys.createMemoObject({src:this,from:from_item,to:to_item});
			setTimeout((function(){Event.fire(this,'xlv:select_changed',memo);}).bind(this),1);
		},
		select_item:function(item,css,fire){
			var p=this.select_properties;
			if(!item&&p._items.length==0)return;
			var old=p.singleSelection?p._items[0]:[].concat(p._items);
			if(item&&p._items.include(item)){
				if(p.singleSelection) return;
				else this.select_clear(item,css,false);
			}else{
				if(p.singleSelection)this.select_clearAll(css,false);
				if(item){
					p._items.push(item);
					Element.addClassName(item,css||p.cssClassName);
				}
			}
			if(fire!==false)this.select_changed(this.select_getSelected(),old);
		},
		select_clear:function(item,css,fire){
			var p=this.select_properties;
			if(item){
				p._items=p._items.without(item); 
				Element.removeClassName(item,css||p.cssClassName);
			}
			if(fire!==false)this.select_changed(null,item);
		},
		select_clearAll:function(css,fire){
			var p=this.select_properties;
			var old=p.singleSelection?p._items[0]:[].concat(p._items);
			for(var i=0,n=p._items.length;i<n;++i){
				Element.removeClassName(p._items[i],css||p.cssClassName);
			}
			p._items.clear();
			if(fire!==false)this.select_changed(null,old);
		},
		select_getSelected:function(){
			var p=this.select_properties;
			return p.singleSelection ? p._items[0] : p._items;
		}
	}
};

